<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择用户性别" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="帐号状态" prop="status">
        <el-input
          v-model="queryParams.status"
          placeholder="请输入帐号状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后登录时间" prop="recentLoginDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.recentLoginDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择最后登录时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="微信OpenID" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入微信OpenID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信UnionID" prop="unionid">
        <el-input
          v-model="queryParams.unionid"
          placeholder="请输入微信UnionID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会话Key" prop="sessionKey">
        <el-input
          v-model="queryParams.sessionKey"
          placeholder="请输入会话Key"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主认证状态" prop="ownerAuthStatus">
        <el-select v-model="queryParams.ownerAuthStatus" placeholder="请选择业主认证状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.auth_binding_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小区名称" prop="bindingVillageName">
        <el-input
          v-model="queryParams.bindingVillageName"
          placeholder="请输入小区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:wx_user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:wx_user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:wx_user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['business:wx_user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wx_userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="微信用户ID" align="center" prop="userId" />
      <el-table-column label="用户账号" align="center" prop="userName" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="小区名称" align="center" prop="bindingVillageName" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="用户性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="帐号状态" align="center" prop="status" />
      <el-table-column label="最后登录时间" align="center" prop="recentLoginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recentLoginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="微信OpenID" align="center" prop="openid" />
      <el-table-column label="微信UnionID" align="center" prop="unionid" />
      <el-table-column label="会话Key" align="center" prop="sessionKey" />
      <el-table-column label="业主认证状态" align="center" prop="ownerAuthStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.auth_binding_status" :value="scope.row.ownerAuthStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:wx_user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:wx_user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微信用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form disabled ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="用户性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择用户性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头像地址">
          <imageUpload v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="帐号状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入帐号状态" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="最后登录时间" prop="recentLoginDate">
          <el-date-picker clearable size="small"
                          v-model="form.recentLoginDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择最后登录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="国家" prop="country">
          <el-input v-model="form.country" placeholder="请输入国家" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="语言" prop="language">
          <el-input v-model="form.language" placeholder="请输入语言" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="微信开放id" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入微信开放id" />
        </el-form-item>
        <el-form-item label="微信公众平台唯一id" prop="unionid">
          <el-input v-model="form.unionid" placeholder="请输入微信公众平台唯一id" />
        </el-form-item>
        <el-form-item label="会话Key值" prop="sessionKey">
          <el-input v-model="form.sessionKey" placeholder="请输入会话Key值" />
        </el-form-item>
        <el-form-item label="系统用户id" prop="sysUserId">
          <el-input v-model="form.sysUserId" placeholder="请输入系统用户id" />
        </el-form-item>
        <el-form-item label="业主认证状态" prop="ownerAuthStatus">
          <el-select v-model="form.ownerAuthStatus" placeholder="请选择业主认证状态">
            <el-option
              v-for="dict in dict.type.auth_binding_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业主认证状态描述" prop="ownerAuthStatusDes">
          <el-input v-model="form.ownerAuthStatusDes" placeholder="请输入业主认证状态描述" />
        </el-form-item>
        <el-form-item label="企业认证状态">
          <el-radio-group v-model="form.companyAuthStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业认证状态描述" prop="companyAuthStatusDes">
          <el-input v-model="form.companyAuthStatusDes" placeholder="请输入企业认证状态描述" />
        </el-form-item>
        <el-form-item label="绑定小区ID" prop="bindindVillageId">
          <el-input v-model="form.bindindVillageId" placeholder="请输入绑定小区ID" />
        </el-form-item>
        <el-form-item label="绑定小区名称" prop="bindingVillageName">
          <el-input v-model="form.bindingVillageName" placeholder="请输入绑定小区名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWx_user, getWx_user, delWx_user, addWx_user, updateWx_user, exportWx_user } from "@/api/business/wx_user";

export default {
  name: "Wx_user",
  dicts: ['sys_user_sex', 'auth_binding_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信用户表格数据
      wx_userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        nickName: null,
        email: null,
        phone: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        recentLoginDate: null,
        country: null,
        province: null,
        language: null,
        city: null,
        openid: null,
        unionid: null,
        sessionKey: null,
        sysUserId: null,
        ownerAuthStatus: null,
        ownerAuthStatusDes: null,
        companyAuthStatus: null,
        companyAuthStatusDes: null,
        bindindVillageId: null,
        bindingVillageName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询微信用户列表 */
    getList() {
      this.loading = true;
      listWx_user(this.queryParams).then(response => {
        this.wx_userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        userName: null,
        nickName: null,
        email: null,
        phone: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        delFlag: null,
        recentLoginDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        country: null,
        province: null,
        language: null,
        city: null,
        openid: null,
        unionid: null,
        sessionKey: null,
        sysUserId: null,
        ownerAuthStatus: null,
        ownerAuthStatusDes: null,
        companyAuthStatus: "0",
        companyAuthStatusDes: null,
        bindindVillageId: null,
        bindingVillageName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getWx_user(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微信用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateWx_user(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWx_user(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除微信用户编号为"' + userIds + '"的数据项？').then(function() {
        return delWx_user(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有微信用户数据项？').then(() => {
        this.exportLoading = true;
        return exportWx_user(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
